package com.mxpio.erp.plan.controller;

import com.mxpio.erp.common.plan.entity.*;
import com.mxpio.erp.common.plan.service.SalePredictionOffsettingFlowService;
import com.mxpio.erp.common.plan.service.SalePredictionOffsettingFlowVOService;
import com.mxpio.erp.common.plan.vo.SalePredictionOffsettingFlowVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "SalePredictionOffsettingFlowController", description = "销售预测冲销流水")
@RestController
@RequestMapping("/erp/plan/spof")
public class SalePredictionOffsettingFlowController {

	@Autowired
	private SalePredictionOffsettingFlowService salePredictionOffsettingFlowService;
	@Autowired
	private SalePredictionOffsettingFlowVOService salePredictionOffsettingFlowVOService;
	@GetMapping("list")
	@Operation(summary = "销售预测冲销流水列表", description = "获取销售预测冲销流水列表（无分页）", method = "GET")
	public Result<List<SalePredictionOffsettingFlow>> list(@Parameter(description = "查询参数") Criteria criteria)
			throws UnsupportedEncodingException {
		List<SalePredictionOffsettingFlow> salePredictionOffsettingFlow = salePredictionOffsettingFlowService.list(SalePredictionOffsettingFlow.class, criteria);
		return Result.OK(salePredictionOffsettingFlow);
	}
	
	@GetMapping("page")
	@Transactional
	@Operation(summary = "销售预测冲销流水列表", description = "获取销售预测冲销流水列表（分页）", method = "GET")
	public Result<Page<SalePredictionOffsettingFlow>> page(@Parameter(description = "查询参数") Criteria criteria,
			@Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo)
			throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		Page<SalePredictionOffsettingFlow> salePredictionOffsettingFlow = salePredictionOffsettingFlowService.listPage(page, criteria);
		return Result.OK(salePredictionOffsettingFlow);
	}

	@GetMapping("vopage")
	@Transactional
	@Operation(summary = "销售预测冲销流水列表", description = "获取销售预测冲销流水列表（分页）", method = "GET")
	public Result<Page<SalePredictionOffsettingFlowVO>> vopage(@Parameter(description = "查询参数") Criteria criteria,
														   @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo)
			throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		Page<SalePredictionOffsettingFlowVO> salePredictionOffsettingFlow = salePredictionOffsettingFlowVOService.listPage(page, criteria);
		return Result.OK(salePredictionOffsettingFlow);
	}

	@RequestMapping("save")
	@Operation(summary = "保存销售预测冲销流水", description = "保存销售预测冲销流水")
	public Result<List<SalePredictionOffsettingFlow>> save(@RequestBody List<SalePredictionOffsettingFlow> salePredictionOffsettingFlows) {
		salePredictionOffsettingFlowService.handleSaveData(salePredictionOffsettingFlows);//关联客户
		salePredictionOffsettingFlowService.save(salePredictionOffsettingFlows);
		return Result.OK(salePredictionOffsettingFlows);
	}
	@DeleteMapping("remove/{ids}")
	@Operation(summary = "根据ids删除冲销流水", description = "删除冲销流水", method = "DELETE")
	@Transactional(readOnly = false)
	public Result<?> remove(
			@Parameter(description = "冲销流水id，多个单号用英文逗号分隔") @PathVariable(name = "ids", required = true) String ids) {
		String[] idArray = ids.split(",");
		for (String key : idArray) {
			salePredictionOffsettingFlowService.delete(SalePredictionOffsettingFlow.class, key);
		}
		return Result.OK();
	}
}
